package io.sage.modules.statistics.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.sage.common.utils.R;
import io.sage.modules.statistics.entity.BStatisticsHumidity;
import io.sage.modules.statistics.service.BStatisticsHumidityService;
import io.sage.modules.statistics.vo.BStatisVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 数据统计湿度表
 *
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2019-08-07 18:31:07
 */
@RestController
@RequestMapping("statistics/bstatisticshumidity")
public class BStatisticsHumidityController {

    @Autowired
    private BStatisticsHumidityService bStatisticsHumidityService;

    /**
     * 湿度列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("statistics:bstatisticshumidity:list")
    public R list(@RequestParam Map<String, Object> params){
    	Integer pageSize = 1;
		if (params != null && params.get("limit") != null) {
			pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
		}
		Integer pageNum = 10;
		if (params != null && params.get("page") != null) {
			pageNum = java.lang.Integer.parseInt(params.get("page").toString());
		}

		HashMap<String, Object> paramMap = new HashMap<String, Object>();

		PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小
		List<BStatisticsHumidity> selectStatisticsHumidityList = bStatisticsHumidityService.selectStatisticsHumidityList(paramMap);

		PageInfo<BStatisticsHumidity> pageInfo = new PageInfo<BStatisticsHumidity>(selectStatisticsHumidityList);// 传入list就可以了

		return R.ok().put("statisticsHumidityList", pageInfo);
    }

	/**
	 * 查询指定时间内平均湿度，比如查询最近12小时内每小时的平均湿度
	 * 需要三个参数:
	 * warehouseId: 库房id，必须
	 * dateFormat: 非必须参数，默认为'%Y-%m-%d %H'。mysql 的时间格式，查询时按时间格式分组，比如值为'%Y-%m-%d %H'时，按小时分组，然后取平均值
	 * dateLimit: 非必须参数，默认12。查询条数限制，比如12，只查询最后12条
	 */
	@RequestMapping(value="/getAvgByWarehouse", method={RequestMethod.POST, RequestMethod.GET})
	@RequiresPermissions("statistics:bstatisticshumidity:list")
	public R getAvgByWarehouse(@RequestParam Map<String, Object> params){
		if(params == null) {
			return R.error("缺少必须参数：仓库id。");
		}

		if (params.get("warehouseId") == null) {
			return R.error("仓库id无效。");
		}

		Integer warehouseId = Integer.parseInt(params.get("warehouseId").toString());

		HashMap<String, Object> paramMap = new HashMap<>();
		paramMap.put("warehouseId", warehouseId);

		// 取出时间间隔参数，如果有，没有就使用默认值
		String dateFormat = "%Y-%m-%d %H";
		if (params.get("dateFormat") != null) {
			dateFormat = params.get("dateFormat").toString();
		}
		Integer dateLimit = 12;  //限定查询最近几条的记录，默认12
		if (params.get("dateLimit") != null) {
			dateLimit = Integer.parseInt(params.get("dateLimit").toString());
		}

		//添加时间间隔参数
		paramMap.put("dateFormat", dateFormat);
		paramMap.put("dateLimit",dateLimit);

		List<BStatisVo> bStatisVoList = bStatisticsHumidityService.selectListByWarehouseId(paramMap);  //默认查询出的集合是倒序的

		//创建用来存储结果的map
		Map<String, Object> resultMap = new HashMap<>();

		//如果没有数据，返回空map
		if(bStatisVoList.size()<1) {
			return R.ok().put("bStatisMap",resultMap);
		}

		//创建存储结果的集合
		List<Date> bStatisTime = new ArrayList<>();
		List<Double> bStatisValue = new ArrayList<>();
		//往集合添加值
		for(int i=bStatisVoList.size()-1; i>-1; i--) {
			bStatisTime.add(bStatisVoList.get(i).getTime());
			bStatisValue.add(bStatisVoList.get(i).getAvg());
		}
		resultMap.put("bStatisTime",bStatisTime);
		resultMap.put("bStatisValue",bStatisValue);
		resultMap.put("unit",bStatisVoList.get(0).getUnit()); //取得单位

		return R.ok().put("bStatisMap",resultMap);
	}

    /**
     * 测试单条数据保存
     */
    @RequestMapping("/save")
	public R save(@RequestBody JSONObject jsonObject){
    	BStatisticsHumidity statisticsHumidity1 = new BStatisticsHumidity();
    	statisticsHumidity1.setCreateTime(new Date());
    	statisticsHumidity1.setIsDeleted(0);
    	statisticsHumidity1.setIsUsed(1);
    	statisticsHumidity1.setMachineId(2);
    	statisticsHumidity1.setRemark("备注66");
    	statisticsHumidity1.setUnit("℃");
    	statisticsHumidity1.setValue(21.2);

		int insert = bStatisticsHumidityService.insert(statisticsHumidity1);

		if(insert>0){
			return R.ok();
		}

		return R.error();
	}

    /**
     * 测试批量议添加湿度数据
     */
    @RequestMapping("/saveList")
	public R saveList(@RequestBody JSONObject jsonObject){
    	BStatisticsHumidity statisticsHumidity1 = new BStatisticsHumidity();
    	statisticsHumidity1.setCreateTime(new Date());
    	statisticsHumidity1.setIsDeleted(0);
    	statisticsHumidity1.setIsUsed(1);
    	statisticsHumidity1.setMachineId(2);
    	statisticsHumidity1.setRemark("备注");
    	statisticsHumidity1.setUnit("℃");
    	statisticsHumidity1.setValue(21.2);
    	BStatisticsHumidity statisticsHumidity2 = new BStatisticsHumidity();
    	statisticsHumidity2.setCreateTime(new Date());
    	statisticsHumidity2.setIsDeleted(0);
    	statisticsHumidity2.setIsUsed(1);
    	statisticsHumidity2.setMachineId(2);
    	statisticsHumidity2.setRemark("备注121");
    	statisticsHumidity2.setUnit("℃");
    	statisticsHumidity2.setValue(56.2);
    	List<BStatisticsHumidity> saList=new ArrayList<BStatisticsHumidity>();
    	saList.add(statisticsHumidity1);
    	saList.add(statisticsHumidity2);
		int insert = bStatisticsHumidityService.saveHumidityList(saList);

		if(insert>0){
			return R.ok();
		}

		return R.error();
	}

}
